package com.colorful.insuranceproject.mine;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.base.TitleActivity;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.request.UserRequest;
import com.lovely3x.common.utils.Response;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by handsome-Bob on 2017/8/21.
 * <p>
 * 修改密码
 */
public class ModifyLoginPwdActivity extends TitleActivity {

    private static final int MODIFY_LOGIN_PWD_URL = 0X1;

    private UserRequest userRequest;

    @Bind(R.id.et_pwd)
    EditText etPwd;

    @Bind(R.id.et_pwd_again)
    EditText etPwdAgain;

    @Bind(R.id.img_show_one)
    ImageView imgShowOne;

    @Bind(R.id.img_show_two)
    ImageView imgShowTwo;

    private int judgeIconOld;

    private int judgeIconNew;

    @Override
    protected int getContentView() {
        return R.layout.activity_modify_login_pwd;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setTitle(getString(R.string.modify_pwd));
    }

    @Override
    protected void onViewInitialized() {
        userRequest = new UserRequest(getHandler());
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        switch (msg.what) {
            case MODIFY_LOGIN_PWD_URL:
                if (response.isSuccessful) {
                    showToast(getString(R.string.set_login_pwd_success));
                    launchLoginActivity();
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;
        }
    }

    @OnClick(R.id.tv_confirm)
    void OnClicked() {
        String pwd = etPwd.getText().toString().trim();
        String pwd2 = etPwdAgain.getText().toString().trim();
        if (pwd.length() > 20 || pwd.length() < 6) {
            showToast(getString(R.string.old_pwd_input_error));
        } else if (pwd2.length() > 20 || pwd2.length() < 6) {
            showToast(getString(R.string.set_pwd_length_of_6_to_20));
        } else {
            userRequest.modifyLoginPasswordUrl(pwd, pwd2, MODIFY_LOGIN_PWD_URL);
        }
    }

    /**
     * 页面监听处理
     */
    @OnClick({R.id.img_show_one, R.id.img_show_two})
    void OnClicked(View view) {
        switch (view.getId()) {
            case R.id.img_show_one:
                if (judgeIconOld == 0) {
                    etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    judgeIconOld = 1;
                    imgShowOne.setImageResource(R.drawable.icon_bukejian);
                } else {
                    etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    judgeIconOld = 0;
                    imgShowOne.setImageResource(R.drawable.icon_kejian);
                }
                if (etPwd.length() > 1) {
                    etPwd.setSelection(etPwd.length());
                }
                break;
            case R.id.img_show_two:
                if (judgeIconNew == 0) {
                    etPwdAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    judgeIconNew = 1;
                    imgShowTwo.setImageResource(R.drawable.icon_bukejian);
                } else {
                    etPwdAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    judgeIconNew = 0;
                    imgShowTwo.setImageResource(R.drawable.icon_kejian);
                }
                if (etPwdAgain.length() > 1) {
                    etPwdAgain.setSelection(etPwdAgain.length());
                }
                break;
        }
    }

}
